$(function(){
	
	var pass1 = $('.formBox #password1'),
		pass2 = $('.formBox #password2'),
		email = $('.formBox #email'),
		age = $('.formBox #age'),
		firstname = $('.formBox #firstname'),
		name = $('.formBox #nom'),
		objet = $('.formBox #objet'),
		lastname = $('.formBox #lastname'),
		arrow = $('.formBox .arrow');

	// Empty the fields on load
	$('#main .row input').val('');

	name.on('blur',function(){
		// Very simple validation
		if (!name.val().match(/^[a-zA-Z\s-]+$/i)){
			name.parent().addClass('error').removeClass('success');
		}
		else{
			name.parent().removeClass('error').addClass('success');
		}
	});
	objet.on('blur',function(){
		// Very simple validation
		if (objet.val() == ''){
			objet.parent().addClass('error').removeClass('success');
		}
		else{
			objet.parent().removeClass('error').addClass('success');
		}
	});
	// Validate the firstname
	
	firstname.on('blur',function(){
		
		// Very simple validation
		if (!firstname.val().match(/^[a-zA-Z\s-]+$/i)){
			firstname.parent().addClass('error').removeClass('success');
		}
		else{
			firstname.parent().removeClass('error').addClass('success');
		}
	});

	// Validate the lastname
	lastname.on('blur',function(){
		
		// Very simple validation
		if (!lastname.val().match(/^[a-zA-Z\s-]+$/i)){
			lastname.parent().addClass('error').removeClass('success');
		}
		else{
			lastname.parent().removeClass('error').addClass('success');
		}
	});

	// Validate the numeric age
	age.on('blur',function(){
		
		// Very simple validation
		if (isNaN(age.val()) == true || (age.val()=="")){
			age.parent().addClass('error').removeClass('success');
		}
		else{
			age.parent().removeClass('error').addClass('success');
		}
		
	});

	// Validate the email field
	email.on('blur',function(){
		
		// Very simple validation
		if (!/^\S+@\S+\.\S+$/.test(email.val())){
			email.parent().addClass('error').removeClass('success');
		}
		else{
			email.parent().removeClass('error').addClass('success');
		}
		
	});

	// Use the complexify plugin on the first password field
	pass1.complexify({minimumChars:6, strengthScaleFactor:0.7}, function(valid, complexity){
		
		if(valid){
			pass2.removeAttr('disabled');
			
			pass1.parent()
					.removeClass('error')
					.addClass('success');
		}
		else{
			pass2.attr('disabled','true');
			
			pass1.parent()
					.removeClass('success')
					.addClass('error');
		}
		
		var calculated = (complexity/100)*268 - 134;
		var prop = 'rotate('+(calculated)+'deg)';
		
		// Rotate the arrow
		arrow.css({
			'-moz-transform':prop,
			'-webkit-transform':prop,
			'-o-transform':prop,
			'-ms-transform':prop,
			'transform':prop
		});
	});
	
	// Validate the second password field
	pass2.on('keydown input',function(){
		
		// Make sure its value equals the first's
		if(pass2.val() == pass1.val()){
			
			pass2.parent()
					.removeClass('error')
					.addClass('success');
		}
		else{
			pass2.parent()
					.removeClass('success')
					.addClass('error');
		} 
	});
	
});
